package com.rizzo.web.delegator;

import com.rizzo.back.configuration.Address;
import com.rizzo.back.configuration.Configuration;
import com.rizzo.back.configuration.PossibleLocales;
import com.rizzo.back.exception.ApplicationException;
import com.rizzo.back.service.ConfigurationService;
import com.rizzo.web.util.LocaleEnum;

import java.util.List;
import java.util.Locale;

/**
 * @author Jan Lievens
 */
public class UtilDelegator {

    /**
     * Field configurationService
     */
    private ConfigurationService configurationService;

    /**
     * Method getMetaDescription returns the metaDescription of this UtilDelegator object.
     *
     * @return the metaDescription (type String) of this UtilDelegator object.
     * @throws ApplicationException
     *
     */
    public String getMetaDescription() throws ApplicationException {
        Configuration configuration = this.configurationService.getConfiguration();
        return configuration.getMeta().getDescription();
    }

    /**
     * Method getMetaKeywords returns the metaKeywords of this UtilDelegator object.
     *
     * @return the metaKeywords (type String) of this UtilDelegator object.
     * @throws ApplicationException when
     */
    public String getMetaKeywords() throws ApplicationException {
        Configuration configuration = this.configurationService.getConfiguration();
        return configuration.getMeta().getKeywords();
    }

    /**
     * Method getPossibleLocales returns the possibleLocales of this UtilDelegator object.
     *
     * @return the possibleLocales (type List<String>) of this UtilDelegator object.
     * @throws ApplicationException when
     */
    public List<String> getPossibleLocales() throws ApplicationException {
        Configuration configuration = this.configurationService.getConfiguration();
        PossibleLocales possibleLocales = configuration.getAdmin().getPreferences().getPossibleLocales();
        return possibleLocales.getLocale();
    }

    /**
     * Method getMaxEntriesPageDatatable returns the maxEntriesPageDatatable of this UtilDelegator object.
     *
     * @return the maxEntriesPageDatatable (type String) of this UtilDelegator object.
     */
    public Integer getMaxEntriesPageDatatable() throws ApplicationException {
        Configuration configuration = this.configurationService.getConfiguration();
        return configuration.getDatatable().getMaxEntriesPage().intValue();
    }

    /**
     * Method getDefaultLocale returns the defaultLocale of this UtilDelegator object.
     *
     * @return the defaultLocale (type Locale) of this UtilDelegator object.
     * @throws ApplicationException when
     */
    public Locale getDefaultLocale() throws ApplicationException {
        Configuration configuration = this.configurationService.getConfiguration();
        return LocaleEnum.valueOf(configuration.getAdmin().getPreferences().getDefaultLocale().toUpperCase()).getLocale();
    }

    /**
     * Method getLocaleSelectionVisible returns the localeSelectionVisible of this UtilDelegator object.
     *
     * @return the localeSelectionVisible (type Boolean) of this UtilDelegator object.
     * @throws ApplicationException when
     */
    public Boolean getLocaleSelectionVisible() throws ApplicationException {
        Configuration configuration = this.configurationService.getConfiguration();
        return configuration.getAdmin().getPreferences().isLocaleSelectionVisible();
    }

    /**
     * Method getHtmlAdminAddress returns the htmlAdminAddress of this UtilDelegator object.
     *
     * @return the htmlAdminAddress (type String) of this UtilDelegator object.
     * @throws ApplicationException when
     */
    public String getHtmlAdminAddress() throws ApplicationException {
        StringBuilder htmlAdminAddress = new StringBuilder();
        Configuration configuration = this.configurationService.getConfiguration();
        Address address = configuration.getAdmin().getAddress();
        htmlAdminAddress.append("<p>").append(address.getName()).append("<br />");
        htmlAdminAddress.append(address.getStreet()).append(" ").append(address.getNumber()).append("<br />");
        htmlAdminAddress.append(address.getZipCode()).append(" ").append(address.getCity()).append("<br />");
        htmlAdminAddress.append(address.getCountry()).append("</p>");
        return htmlAdminAddress.toString();
    }

    /**
     * Method getConfiguration returns the configuration of this UtilDelegator object.
     *
     * @return the configuration (type Configuration) of this UtilDelegator object.
     * @throws ApplicationException when
     */
    public Configuration getConfiguration() throws ApplicationException {
        return this.configurationService.getConfiguration();
    }

    /**
     * Method setConfigurationService sets the configurationService of this UtilDelegator object.
     *
     * @param configurationService the configurationService of this UtilDelegator object.
     */
    public void setConfigurationService(ConfigurationService configurationService) {
        this.configurationService = configurationService;
    }
}
